home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Sessions / Movieless in QuickTime / SimpleDecompress / SimpleDecompress.c next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  2.9 KB  |  139 lines  |  [TEXT/CWIE]

  1. #include <MacTypes.h>
  2. #include <MacMemory.h>
  3. #include <Quickdraw.h>
  4. #include <Fonts.h>
  5. #include <Events.h>
  6. #include <Menus.h>
  7. #include <MacWindows.h>
  8. #include <TextEdit.h>
  9. #include <Dialogs.h>
  10. #include <OSUtils.h>
  11. #include <ToolUtils.h>
  12. #include <SegLoad.h>
  13. #include <Processes.h>
  14. #include <Movies.h>
  15. #include <QuickTimeComponents.h>
  16. #include <StandardFile.h>
  17. #include <NumberFormatting.h>
  18. #include <Files.h>
  19. #include <Endian.h>
  20.  
  21.  
  22. void Initialize(void);
  23. void DecompressSomething( FSSpec *inFile, FSSpec *outFile, short outScript );
  24. void DoDecompress(void);
  25.  
  26.  
  27. void main(void)
  28. {
  29.     Initialize();
  30.     
  31.     DoDecompress();
  32. }
  33.  
  34.  
  35. void Initialize(void)
  36. {
  37.     InitGraf(&qd.thePort);
  38.     InitFonts();
  39.     InitWindows();
  40.     InitMenus();
  41.     TEInit();
  42.     InitDialogs(nil);
  43.     InitCursor();
  44. }
  45.  
  46. void DecompressSomething( FSSpec *inFile, FSSpec *outFile, short outScript )
  47. {
  48.     OSErr err = 0;
  49.     DataDecompressorComponent ci = 0;
  50.     short fref = 0;
  51.     long uncompressedLength = 0;
  52.     Handle uncompressedH = nil;
  53.     long compressedLength = 0;
  54.     Handle compressedH = nil;
  55.     unsigned long ignoreThis = 0;
  56.     long count;
  57.     
  58.     err = FSpOpenDF( inFile, fsRdPerm, &fref );
  59.     if( err ) goto bail;
  60.     
  61.     err = GetEOF( fref, &compressedLength );
  62.     if( err ) goto bail;
  63.     
  64.     // First four bytes are the size of the uncompressed data.
  65.     count = 4;
  66.     err = FSRead( fref, &count, &uncompressedLength );
  67.     if( err ) goto bail;
  68.     uncompressedLength = EndianU32_BtoN( uncompressedLength );
  69.     
  70.     compressedLength -= 4;
  71.     
  72.     compressedH = NewHandle( compressedLength );
  73.     err = MemError();
  74.     if( err ) goto bail;
  75.     HLock( compressedH );
  76.     
  77.     uncompressedH = NewHandle( uncompressedLength );
  78.     err = MemError();
  79.     if( err ) goto bail;
  80.     HLock( uncompressedH );
  81.     
  82.     count = compressedLength;
  83.     err = FSRead( fref, &count, *compressedH );
  84.     if( err ) goto bail;
  85.     
  86.     FSClose( fref );
  87.     fref = 0;
  88.     
  89.     err = FSpDelete( outFile );
  90.     if( err == fnfErr ) err = noErr;
  91.     if( err ) goto bail;
  92.     
  93.     err = FSpCreate( outFile, 'CWIE', 'TEXT', outScript );
  94.     if( err ) goto bail;
  95.     
  96.     err = FSpOpenDF( outFile, fsWrPerm, &fref );
  97.     if( err ) goto bail;
  98.     
  99.     err = OpenADefaultComponent( DataDecompressorComponentType, zlibDataCompressorSubType, &ci );
  100.     if( err ) goto bail;
  101.     
  102.     err = DataCodecDecompress( ci, 
  103.             *compressedH, compressedLength, 
  104.             *uncompressedH, uncompressedLength );
  105.     if( err ) goto bail;
  106.     
  107.     count = uncompressedLength;
  108.     err = FSWrite( fref, &count, *uncompressedH );
  109.     if( err ) goto bail;
  110.     
  111. bail:
  112.     if( fref )
  113.         FSClose( fref );
  114.     if( ci )
  115.         CloseComponent( ci );
  116.     if( uncompressedH )
  117.         DisposeHandle( uncompressedH );
  118.     if( compressedH )
  119.         DisposeHandle( compressedH );
  120.     
  121.     if( err )
  122.         DebugStr( "\p oops." );
  123. }
  124.  
  125.  
  126. void DoDecompress(void)
  127. {
  128.     StandardFileReply    inreply, outreply;
  129.     OSType                list[1] = { 'ZOT ' };
  130.     
  131.     StandardGetFile( nil, 1, list, &inreply );
  132.     if( !inreply.sfGood ) return;
  133.     
  134.     StandardPutFile( "\pSave decompressed file as:", "\puntitled", &outreply );
  135.     if( !outreply.sfGood ) return;
  136.     
  137.     DecompressSomething( &inreply.sfFile, &outreply.sfFile, outreply.sfScript );
  138. }
  139.